package com.gold.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gold.entity.AdminUser;
import com.gold.entity.Exchange;
import com.gold.entity.Province;
import com.gold.entity.Status;
import com.gold.service.ExchangeService;
import com.gold.service.ProvinceService;
import com.gold.service.StatusService;
import com.gold.util.Myutil;

@Controller
public class ExchangeController {

	@Autowired
	private ExchangeService es;
	@Autowired
	private ProvinceService ps;
	@Autowired
	private StatusService ss;

	@RequestMapping("getAllExchangeByPage")
	public String getAllExchangeByPage(String nowPageString, ModelMap mm, HttpServletRequest request) {
		AdminUser oldau = (AdminUser) request.getSession().getAttribute("adminLoginUser");
		if (oldau == null) {
			return "redirect:gold/index.jsp";
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("nowPageString", nowPageString);
		List<Exchange> exchangeList = es.selectAllExchangeByPage(map);
		List<Province> provinceList = ps.selectAllProvince();
		List<Province> addProvinceList = ps.selectSomeProvince();
		List<Status> exchangeStatusList = ss.selectAllExchangeStatus();
		mm.addAttribute("exchangeList", exchangeList);
		mm.addAttribute("provinceList", provinceList);
		mm.addAttribute("addProvinceList", addProvinceList);
		mm.addAttribute("exchangeStatusList", exchangeStatusList);
		mm.addAttribute("nowPage", map.get("nowPageString"));
		return "/gold/exchange/exchange.jsp";
	}

	@RequestMapping("insertExchange")
	public String insertExchange(Exchange exchange, String exchangeChargeString, ModelMap mm) {
		if (Myutil.checkNumberIsNumber(exchangeChargeString)) {
			exchange.setExchangeCharge(Integer.parseInt(exchangeChargeString));
			es.insertExchange(exchange);
		} else {
			mm.addAttribute("msg", "添加失败：整数！");
		}
		return "getAllExchangeByPage.do";
	}

	@RequestMapping("getExchangeByProvId")
	public String getExchangeByProvId(String provId, ModelMap mm) {
		List<Exchange> exchangeList = es.selectExchangeByProvId(Integer.parseInt(provId));
		List<Province> provinceList = ps.selectAllProvince();
		List<Province> addProvinceList = ps.selectSomeProvince();
		List<Status> exchangeStatusList = ss.selectAllExchangeStatus();
		mm.addAttribute("exchangeList", exchangeList);
		mm.addAttribute("provinceList", provinceList);
		mm.addAttribute("addProvinceList", addProvinceList);
		mm.addAttribute("exchangeStatusList", exchangeStatusList);
		return "/gold/exchange/exchange.jsp";
	}

	@RequestMapping("deleteExchange")
	public String deleteExchange(String[] provId, ModelMap mm) {
		es.deleteExchange(provId);
		return "getAllExchangeByPage.do";
	}

	@RequestMapping("toUpdateExchangeJsp")
	public String toUpdateExchangeJsp(int exchangeId, ModelMap mm) {
		Exchange exchange = es.selectExchangeByExchangeId(exchangeId);
		List<Status> exchangeStatusList = ss.selectAllExchangeStatus();
		mm.addAttribute("exchange", exchange);
		mm.addAttribute("exchangeStatusList", exchangeStatusList);
		return "/gold/exchange/updateExchange.jsp";
	}

	@RequestMapping("updateExchange")
	public String updateExchange(Exchange exchange, String exchangeChargeString, ModelMap mm) {
		if (Myutil.checkNumberIsNumber(exchangeChargeString)) {
			exchange.setExchangeCharge(Integer.parseInt(exchangeChargeString));
			es.updateExchange(exchange);
		} else {
			mm.addAttribute("msg", "必须输入整数");
		}
		return "getAllExchangeByPage.do";
	}

}
